package com.ruoyi.consumer.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

public class PictureConverter {
    public static String convertStringToJsonArray(String input) {
        String[] resultArray = input.split(",");
        for (int i = 0; i < resultArray.length; i++) {
            resultArray[i] = resultArray[i].trim();
        }

        // 使用 Jackson 库将数组转换为 JSON 格式字符串
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(resultArray);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String convertJsonArrayToString(String jsonArray) {
        try {
            // 使用 Jackson 库解析 JSON 数组字符串为字符串数组
            ObjectMapper objectMapper = new ObjectMapper();
            String[] stringArray = objectMapper.readValue(jsonArray, String[].class);

            // 使用 String.join 方法将字符串数组连接为一个逗号分隔的字符串
            return String.join(",", stringArray);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        String jsonArray = "[\"http://124.70.218.167:8080/profile/upload/2023/11/14/21104155540_20231114161735A006.jpg\",\"http://124.70.218.167:8080/profile/upload/2023/11/14/earth_20231114161736A007.png\"]";

        String resultString = convertJsonArrayToString(jsonArray);

        // 打印连接后的字符串
        System.out.println(resultString);
    }
}
